﻿using System;
using System.Windows;
using Microsoft.Practices.Unity;
namespace TciNewsReader_2014_04.Infrastructure
{
    public class BaseDialogInitializer
    {
        private readonly Type _viewType;
        private readonly Type _viewModelType;
        private readonly IUnityContainer _container;
        protected BaseDialogInitializer(IUnityContainer container, Type viewType, Type viewModelType)
        {
            _container = container;
            _viewType = viewType;
            _viewModelType = viewModelType;
        }
        private Window _view;
        public void Initialize()
        {
            _view = _container.Resolve(_viewType) as Window;
            object vm = null;
            if (_viewModelType != null)
            {
                vm = _container.Resolve(_viewModelType);
            }
            if (_view != null)
            {
                if (vm != null)
                {
                    _view.DataContext = vm;
                }
                _view.ShowDialog();
            }
        }
        public void Close()
        {
            if (_view != null)
            {
                _view.Close();
            }
        }
    }
}